define(['moment'], function (moment) {
    return ['$state', '$scope', '$rootScope', '$filter', '$stateParams', 'config', 'MMWService', '$mdDialog', '$timeout','$calculater', '$datePicker',
        function ($state, $scope, $rootScope, $filter, $stateParams, config, $MMWService, $mdDialog, $timeout, $calculater, $datePicker) {

            $scope.init = function(){
                $scope.wo_no = '';
                $scope.urgent_and_tining = [];
                $scope.isSelectAll = false;
            };

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            $scope.loadWoNoList = function(){
                $MMWService.showQueryListPNIG({
                    page_mode: 1,
                    pageRecord:50,
                    condition: [
                        {name:$filter('translate')('common.queryList.wo_c'), column_name:'MONo', query_mode:'2', value:$scope.wo_no}
                    ],
                    header: [
                        {label:$filter('translate')('common.queryList.wo_c'), code:'page_no'},
                        {label:$filter('translate')('common.queryList.item_c'), code:'productno'},
                        {label:$filter('translate')('common.queryList.item_n'), code:'productname'},
                        {label:$filter('translate')('common.queryList.wo_no_qty'), code:'moqty', width:'110px'}
                    ],
                    setCondition:function(conditions){
                        conditions.push({column_name:'MOSTATE', query_mode:'0', value:'3', merge_condition_model:'AND' });
                        if($scope.type == '0'){
                            conditions.push({column_name:'UnReleaseLotQty', query_mode:'6', value:'0', merge_condition_model:'AND' });
                        } else if($scope.type == '1'){
                            conditions.push({column_name:'UnReleaseLotQty', query_mode:'5', value:'0', merge_condition_model:'AND' });
                        }
                        return conditions;
                    },
                    selectCode:$scope.wo_no,
                    confirm : function(selectItem){
                        $scope.wo_no = selectItem.page_no;
                        $scope.search();
                    }
                });
            };

            $scope.barcodeScan = function(event){
                if(event.keyCode == 13){
                    $scope.search();
                }
            };

            $scope.search = function(){
                if($scope.wo_no != '')
                {
                    $MMWService.pageNumberInfoGet({
                        page_mode : 'opm_urgent_and_timing_info_get',
                        query_condition_info: [
                            {column_name:'MONO', query_mode:'0', value:$scope.wo_no, merge_condition_model:'AND'}
                        ]
                    }, function(itemList){
                        if(itemList && itemList.length > 0)
                        {
                            $scope.urgent_and_tining = itemList;
                            $scope.urgent_and_tining.forEach(function(item){
                                item.select = false;
                            });
                        } 
                        else
                        {
                            $scope.urgent_and_tining = [];
                            $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                        };
                    });
                }
                else
                {
                    $scope.$root.showAlert($filter('translate')('OPM02.msg.please_enter_the_work_order_no')); // 請選擇工單！
                };
            };

            $scope.selectAll = function(){
                if($scope.urgent_and_tining && $scope.urgent_and_tining.length > 0)
                {
                    $scope.isSelectAll = !$scope.isSelectAll;
                    $scope.urgent_and_tining.forEach(function(item){
                        if(!$scope.isSelectAll)
                            item.isSelect = false;
                        else
                            item.isSelect = true;
                    });
                }
            };

            $scope.select = function(item){
                if(!item.isSelect)
                    item.isSelect = true;
                else
                    item.isSelect = false;
            };

            $scope.changeUrgentState = function(item){
                if(item.urgent == '0')
                    item.urgent = '1';
                else 
                    item.urgent = '0';
            };

            $scope.changeTimingState = function(item){
                if(item.timing == '0')
                    item.timing = '1';
                else 
                    item.timing = '0';
            };

            $scope.confirm = function(){
                if($scope.urgent_and_tining && $scope.urgent_and_tining.length > 0)
                {
                    var urgent_and_tining = [];
                    $scope.urgent_and_tining.forEach(function(item){
                        if(item.isSelect)
                            urgent_and_tining.push(item);
                    });
                    if(urgent_and_tining && urgent_and_tining.length > 0)
                    {
                        $MMWService.sendToServer({
                            uri : 'ServicesSTD.Module_OPM.urgent_and_timing_process',
                            content : {
                                urgent_and_tining: urgent_and_tining
                            },
                            success : function(data){
                                $scope.$root.showToast($filter('translate')('common.msg.success'));
                                $scope.urgent_and_tining = [];
                                $scope.isSelectAll = false;
                            }
                        });
                    }
                    else
                        $scope.$root.showAlert($filter('translate')('OPM02.msg.please_select_at_least_one_data')); // 請至少選擇一筆資料！
                }
                else
                    $scope.$root.showAlert($filter('translate')('OPM02.msg.Please_inquire_first')); // 請先進行查詢！
            };

        }
    ];
});
